package com.locus.locus.server.dados;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.locus.locus.client.dados.ConfiguracaoBoxWrapper;
import com.locus.locus.client.dados.ConfiguracaoConjuntoBoxesWrapper;

@PersistenceCapable
public final class ConfiguracaoConjuntoBoxes implements Wrappable<ConfiguracaoConjuntoBoxesWrapper> {
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private List<Key> chavesBoxes;
	
	private transient List<ConfiguracaoBox> boxes;
	
	public ConfiguracaoConjuntoBoxes() {
		this.chavesBoxes = new ArrayList<Key>();
		this.boxes = new ArrayList<ConfiguracaoBox>();
	}
	
	public Key getKey() {
		return key;
	}
	public void setKey(Key key) {
		this.key = key;
	}
	public List<Key> getChavesBoxes() {
		return chavesBoxes;
	}
	public void setChavesBoxes(List<Key> chavesBoxes) {
		this.chavesBoxes = chavesBoxes;
	}
	public List<ConfiguracaoBox> getBoxes() {
		return boxes;
	}
	public void setBoxes(List<ConfiguracaoBox> boxes) {
		this.boxes = boxes;
	}

	@Override
	public void deserializarRelacionamentos(PersistenceManager pm) {
		if(this.chavesBoxes != null && this.chavesBoxes.size() > 0){
			this.boxes = new ArrayList<ConfiguracaoBox>((List<ConfiguracaoBox>) pm.newQuery(ConfiguracaoBox.class, ":p.contains(key)").execute(this.chavesBoxes));
			for(ConfiguracaoBox box:this.boxes){
				box.deserializarRelacionamentos(pm);	
			}
		}else{
			this.boxes = new ArrayList<ConfiguracaoBox>();
		}
	}

	@Override
	public ConfiguracaoConjuntoBoxesWrapper gerarWrapper() {
		final ConfiguracaoConjuntoBoxesWrapper wrapper = new ConfiguracaoConjuntoBoxesWrapper();
		final ArrayList<ConfiguracaoBoxWrapper> boxesWrapper = new ArrayList<ConfiguracaoBoxWrapper>();
		
		if(this.boxes != null){
			for(ConfiguracaoBox b:this.boxes){
				boxesWrapper.add(b.gerarWrapper());
			}
		}

		wrapper.setBoxes(boxesWrapper);
		
		return wrapper;
	}
}
